/**
 * Created by wangzih on 2015/1/20.
 */
function Dialog(wnd,title) {
    this.wnd = wnd;
    this.doc = wnd.document;
    this.title = title;
    this.buttons=[];
    this._init();
}
/**
 * 初始化
 * @private
 */
Dialog.prototype._init = function () {
    this.maindiv = this.doc.createElement("div");
    this.maindiv.style.zIndex = "10";
    this.maindiv.style.border = "1px solid #598BDC";
    this.maindiv.style.position = "absolute";
    this.maindiv.style.left = (this.doc.body.clientWidth / 3) + "px";
    this.maindiv.style.top = (this.doc.body.clientHeight / 3) + "px";
    this.maindiv.style.background = "#FFFFFF";
    this.maindiv.style.borderRadius = "2px";
    this.headdiv = this.doc.createElement("div");
    this.bodydiv = this.doc.createElement("div");
    this.footdiv = this.doc.createElement("div");
    this.maindiv.style.width = "300px";
    this.maindiv.style.height = "200px";
    this.maindiv.appendChild(this.headdiv);
    this.maindiv.appendChild(this.bodydiv);
    this.maindiv.appendChild(this.footdiv);
    this.doc.body.appendChild(this.maindiv);
    this._inithead();
    this._initbody();
    this._initfoot();
    this.maindiv.style.display = "none";
};
/**
 * 初始化头部
 * @private
 */
Dialog.prototype._inithead = function () {
    this._clickdown = false;
    this.headdiv.style.width = "100%";
    this.headdiv.style.height = "32px";
    this.headdiv.style.position="relative";
    this.headdiv.style.textAlign = "left";
    this.headdiv.style.background = "#598BDC";
    this.headarea = this.doc.createElement("span");
    this.headdiv.appendChild(this.headarea);
    this.headarea.innerHTML = this.title ? this.title : "消息";
    this.headarea.style.position = "absolute";
    this.headarea.style.top = "6px";
    this.closearea = this.doc.createElement("span");
    this.closeimg=this.doc.createElement("img");
    this.closeimg.src=realpath+"/plan/utiljs/dialog/dialogcloseimg.png";
    this.closearea.appendChild( this.closeimg);
   // this.closearea.innerHTML = "<img src="realpath+""'>";
    this.closearea.style.cursor = "pointer";
    this.closearea.style.position = "relative";
    this.closearea.style.top = "8px";
    this.closearea.style.float="right";
    this.headdiv.appendChild(this.closearea);
    this.closearea.onclick = this.close.bind(this);
    this.headdiv.onmousedown = function () {
        var e = window.event;
        if((isie&&e.button==1)||(isChrome&&e.button == 0)){
            this.curleft = e.clientX;
            this.curtop = e.clientY;
            this.left = parseInt(this.maindiv.style.left);
            this.top = parseInt(this.maindiv.style.top);
            this._clickdown = true;
            this.headdiv.style.cursor = "move";
        }
    }.bind(this);
    this.headdiv.onmousemove = function () {
        if (this._clickdown) {
            var e = window.event;
            var x = (this.left + e.clientX - this.curleft);
            var y = (this.top + e.clientY - this.curtop);
            this.setPosition(x, y);
        }
    }.bind(this);
    this.headdiv.onmouseup = function () {
        if (this._clickdown) {
            var e = window.event;
            var x = (e.clientX - this.curleft + this.left);
            var y = (e.clientY - this.curtop + this.top);
            this.setPosition(x, y);
            this._clickdown = false;
            this.headdiv.style.cursor = "default";
        }
    }.bind(this);
};
/**
 * 身体
 * @private
 */
Dialog.prototype._initbody = function () {
    this.bodydiv.style.width = "100%";
    this.bodydiv.style.height = "136px";
    this.contenttable=this.doc.createElement("table");
    this.contenttable.style.display="none";
    this.contenttr=this.contenttable.insertRow(-1);
    this.contentimg=this.contenttr.insertCell(-1);
    this.contenttext=this.contenttr.insertCell(-1);
    this.textheadimg=this.doc.createElement("img");
    this.textheadimg.src="message.gif";
    this.textdiv=this.doc.createElement("div");
    this.textdiv.style.display="inline";
    this.textdiv.style.lineHeight="10px";
    this.contentimg.appendChild(this.textheadimg);
    this.contenttext.appendChild(this.textdiv);
    this.contenttable.style.position="relative";
    this.contenttable.style.left=this.bodydiv.clientWidth/10+"px";
    this.contenttable.style.top=this.bodydiv.clientHeight/4+"px";
    this.bodydiv.appendChild(this.contenttable);

};
/**
 * 初始化底部
 * @private
 */
Dialog.prototype._initfoot = function () {
    this.footdiv.style.width = "100%";
    this.footdiv.style.height = "30px";
    this.footdiv.style.textAlign = "right";
    this.footdiv.style.borderTop = "1px solid #E0DEE1";
    this.footdiv.style.borderBottom = "1px solid white";
    this.footdiv.style.background = "white";
};
/**
 * @param value 名字
 * @param onclickfunc 点击事件
 * @x y 位置 相对
 * @returns {HTMLElement} 返回元素
 */
Dialog.prototype.addButton = function (value,x,y,onclickfunc) {
    var button = this.doc.createElement("input");
    button.setAttribute("type", "button");
    button.setAttribute("value", value);
    button.style.position="relative";
    button.style.right=x+"px";
    button.style.top=y+"px";
    button.onclick = onclickfunc;
    this.footdiv.appendChild(button);
    this.buttons.push(button);
    return button;
};
/**
 * 设置button事件
 * @param button
 * @param onclickfunc
 */
Dialog.prototype.setButtonOnClick=function(button,onclickfunc){
    button.onclick=onclickfunc;
};
/**
 * 返回div
 * @returns {HTMLElement|*}
 */
Dialog.prototype.getContent=function(){
  return this.bodydiv;
};
/**
 * 设置title
 * @param title
 */
Dialog.prototype.setTitle=function(title){
    this.headarea.innerHTML=title;
};
/**
 * 设置message
 * @param title
 */
Dialog.prototype.setMessage=function(msg){
    this.textdiv.innerHTML=msg;
};
/**
 * 显示对话框 可以点击其他东西
 */
Dialog.prototype.show = function () {
    this.maindiv.style.display = "block";
};
/**
 *显示对话框 不能点击其他东西
 */
Dialog.prototype.showModel = function () {
    this.hiddenimg = this.doc.createElement("img");
    this.doc.body.appendChild(this.hiddenimg);
    this.hiddenimg.style.zIndex = "9";
    this.hiddenimg.style.height = "100%";
    this.hiddenimg.style.width = "100%";
    this.hiddenimg.style.position = "absolute";
    this.hiddenimg.style.left = "0px";
    this.hiddenimg.style.top = "0px";
    this.hiddenimg.src = realpath+"/plan/utiljs/dialog/null.gif";
    this.maindiv.style.display = "block";

};
/**
 *设置位置
 */
Dialog.prototype.setPosition = function (x, y) {
    this.maindiv.style.left = x + "px";
    this.maindiv.style.top = y + "px";
};
/**
 *设置宽度
 */
Dialog.prototype.setWidth = function (width) {
    this.maindiv.style.width=width;
};
/**
 *设置高度
 */
Dialog.prototype.setHeight = function (height) {
    this.maindiv.style.height=height;
    this.bodydiv.style.height=(parseInt(height)-64)+"px";
};

/**
 *关闭
 */
Dialog.prototype.close = function () {
    this.dispose();
};

/**
 *释放内存
 */
Dialog.prototype.dispose = function () {
    this.doc.body.removeChild(this.maindiv);
    if (this.hiddenimg) {
        this.doc.body.removeChild(this.hiddenimg);
    }
    this.wnd = null;
    this.doc = null;
    this.headdiv.onmousedown = null;
    this.headdiv.onmousemove = null;
    this.headdiv.onmouseup = null;
    this.headarea = null;
    this.closearea.onclick = null;
    this.closearea = null;
    this.contenttable=null;
    this.contenttext=null;
    this.contenttr=null;
    this.contentimg=null;
    this.textdiv=null;
    this.textheadimg=null;
    for(var i=0;i<this.buttons.length;i++){
        this.buttons[i].onclick=null;
        this.buttons[i]=null;
    }
    this.buttons=null;
    this.maindiv = null;
    this.headdiv = null;
    this.bodydiv = null;
    this.footdiv = null;
};
/**
 * 确认框
 * @param wnd
 * @param message
 * @constructor
 */
function ConfirmDialog(wnd,message){
    Dialog.call(this, wnd, "确认");
    this.msg=message;
    this._initConfirmDialog();
};
extendClass(ConfirmDialog, Dialog, "ConfirmDialog");

ConfirmDialog.prototype._initConfirmDialog=function(){
    this.okbtn = this.addButton("确定",16,4,function(){
        alert("ok");
        this.close();
    }.bind(this));
    this.cancelbtn = this.addButton("取消",8,4,function(){
        this.close();
    }.bind(this));
    this.textdiv.innerText= this.msg;
    this.contenttable.style.display="inline";
};


/**
 * 信息提示框
 * @param wnd
 * @param message
 * @constructor
 */
function MessageDialog(wnd, message) {
    Dialog.call(this, wnd, "消息");
    this.msg=message;
    this._initMessageDialog();
}

extendClass(MessageDialog, Dialog, "MessageDialog");

/**
 * 初始化信息框
 * @private
 */
MessageDialog.prototype._initMessageDialog = function () {
    this.textdiv.innerText= this.msg;
    this.contenttable.style.display="inline";
    this.setWidth("350px");
    this.setHeight("150px");
    this.headarea.style.top = "3px";
    this.addButton("确定",0,0,function(){
        this.close();
    }.bind(this));
};